const fileService=require("../service/file.service")
const userService=require('../service/user.service')
const {APP_HOST,APP_PORT}=require('../app/config')

class FileController{
  async saveAvatarInfo(ctx,next){
    const {mimetype,filename,size}=ctx.req.file
    const {id}=ctx.user

    //将图像信息保存到avatar表中
    await fileService.createAvatar(mimetype,filename,size,id)

    //将图片地址保存到user表中
    const avatarUrl=`${APP_HOST}:${APP_PORT}/users/${id}/avatar`;
    await userService.updateAvatarUrlById(id,avatarUrl)

    ctx.body='上传头像成功'
  }

  async savePictureInfo(ctx,next){
    const files=ctx.req.files
    const {id}=ctx.user
    const {momentId}=ctx.query

    for(let file of files){
      const {mimetype,filename,size}=file
      await fileService.createPicture(mimetype,filename,size,momentId,id)
    }

    ctx.body='动态配图上传成功'
  }
}

module.exports=new FileController()